package cn.zust.itcost.controller;

import cn.zust.itcost.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*求职管理*/

@Slf4j
@RestController
@RequestMapping("/job")
public class JobController {
    @Autowired
    private UserService userService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private PredictService predictService;

    @Autowired
    private BM25Service bm25Service;

    @Autowired
    private HandleKnowledgeMap handleKnowledgeMap;

    /**
     * 返回注册数量
     * @return
     */
    @PutMapping("/trend")
    public double[] generateForecast() {
        int numMonths = 12;
        double[] inputData = new double[numMonths];
        LocalDate currentDate = LocalDate.now();

        for (int i = 0; i < numMonths; i++) {
            LocalDate firstDayOfMonth = currentDate.minusMonths(i).withDayOfMonth(1);
            LocalDate lastDayOfMonth = firstDayOfMonth.plusMonths(1).minusDays(1);

            int userCount = userService.getUserCountByMonthRange(firstDayOfMonth, lastDayOfMonth);
            inputData[numMonths - 1 - i] = userCount;
        }

        // 调用arimaForecast方法进行预测
        int numForecasts = 3; // 假设要预测接下来3个月的数据
        double[] forecastValues = predictService.arimaForecast(inputData, numForecasts);

        // 合并前12个月份数据和预测数据到一个数组中
        int totalMonths = numMonths + numForecasts;
        double[] resultData = new double[totalMonths];
        for (int i = 0; i < numMonths; i++) {
            resultData[i] = inputData[i];
        }
        for (int i = 0; i < numForecasts; i++) {
            resultData[numMonths + i] = forecastValues[i];
        }

        return resultData;
    }

    /**
     * 返回求职趋势
     * @return
     */
    @PostMapping("/trend1")
    public double[] generateForecast1(String UserJob) {
        int numMonths = 12;
        double[] inputData = new double[numMonths];
        LocalDate currentDate = LocalDate.now();

        for (int i = 0; i < numMonths; i++) {
            LocalDate firstDayOfMonth = currentDate.minusMonths(i).withDayOfMonth(1);
            LocalDate lastDayOfMonth = firstDayOfMonth.plusMonths(1).minusDays(1);

            int userCount = userService.getUserCountByMonthRangeandUserJob(firstDayOfMonth, lastDayOfMonth,UserJob);
            inputData[numMonths - 1 - i] = userCount;
        }

        // 调用arimaForecast方法进行预测
        int numForecasts = 3; // 假设要预测接下来3个月的数据
        double[] forecastValues = predictService.arimaForecast(inputData, numForecasts);

        // 合并前12个月份数据和预测数据到一个数组中
        int totalMonths = numMonths + numForecasts;
        double[] resultData = new double[totalMonths];
        for (int i = 0; i < numMonths; i++) {
            resultData[i] = inputData[i];
        }
        for (int i = 0; i < numForecasts; i++) {
            resultData[numMonths + i] = forecastValues[i];
        }

        return resultData;
    }

    /**
     * 返回招聘趋势
     * @return
     */
    @PostMapping("/trend2")
    public double[] generateForecast2(String NeedJob) {
        int numMonths = 12;
        double[] inputData = new double[numMonths];
        LocalDate currentDate = LocalDate.now();

        for (int i = 0; i < numMonths; i++) {
            LocalDate firstDayOfMonth = currentDate.minusMonths(i).withDayOfMonth(1);
            LocalDate lastDayOfMonth = firstDayOfMonth.plusMonths(1).minusDays(1);

            int needCount = enterpriseService.getUserCountByMonthRangeandNeedJob(firstDayOfMonth, lastDayOfMonth,NeedJob);
            inputData[numMonths - 1 - i] = needCount;
        }

        // 调用arimaForecast方法进行预测
        int numForecasts = 3; // 假设要预测接下来3个月的数据
        double[] forecastValues = predictService.arimaForecast(inputData, numForecasts);

        // 合并前12个月份数据和预测数据到一个数组中
        int totalMonths = numMonths + numForecasts;
        double[] resultData = new double[totalMonths];
        for (int i = 0; i < numMonths; i++) {
            resultData[i] = inputData[i];
        }
        for (int i = 0; i < numForecasts; i++) {
            resultData[numMonths + i] = forecastValues[i];
        }

        return resultData;
    }

    /**
     * 岗位推荐
     * @param ability
     * @param jobs
     * @return
     */
    @PostMapping("/recommend")
    public double[][] recommendJob(String ability, String[] jobs) {//ability是用户的能力
        // 参数设置
        double sAvg = 5.0;
        double k1 = 1.2;
        double b = 0.75;


        // 自比较
        double result1 = bm25Service.calculateBM25(ability, ability, sAvg, k1, b);

        // 查找得到所有面向岗位为 job 的招聘信息为 jobs[]
        int arrayLength = 5; // count 匹配的数量

        List<Double> results = new ArrayList<>();

        for (String each : jobs) {
            double result = bm25Service.calculateBM25(ability, each, sAvg, k1, b);
            results.add(result);
        }

        // 对结果进行排序
        results.sort(Collections.reverseOrder());

        // 取前三大的匹配度
        int topCount = Math.min(4, results.size());

        double[][] outputArray = new double[topCount][2];

        for (int i = 0; i < topCount; i++) {
            outputArray[i][0] = i;//理论上应该是招聘id
            outputArray[i][1] = (results.get(i) / result1)*100;//匹配度
        }

        return outputArray;
    }


}
